import axios, {AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig} from "axios";

const config: axios.AxiosRequestConfig<any> = {
    baseURL: "http://192.168.20.5:8080/api/v1",
    timeout: 200,
    withCredentials: true,
};

class HttpAxios {
    service: AxiosInstance;

    constructor(config: AxiosRequestConfig) {
        this.service = axios.create(config);
        this.service.interceptors.request.use(
            (config: InternalAxiosRequestConfig) => {
                let language = "tw";
                config.headers = {...config.headers, 'Accept-Language': language}
                config.headers['Accept-Language'] = language
                // config.headers.set('Accept-Language',language,true)
                return config;
            },
            (error: AxiosError) => {
                return Promise.reject(error);
            },
        );
        this.service.interceptors.response.use(
            (resp: AxiosResponse) => {
                return resp.data;
            },
            (error: AxiosError) => {
                const {response} = error;
                return Promise.reject(error);
            },
        );
    }
}

let httpAxios = new HttpAxios(config).service;
export default httpAxios
